<template>
  <h1>猜数字练习</h1>
  <input type="text" placeholder="请输入数字" v-model="num">
  <button @click="cai">猜一猜</button>
  <h3>{{ res }}</h3>
</template>

<script setup>

import {ref} from "vue";

let n = parseInt(Math.random() * 100) + 1;
console.log("答案:" + n)
const num = ref('');
const res = ref('');

const cai = () => {
  const number = parseFloat(num.value);
  if (isNaN(number)) {
    alert('请输入数字')
    return
  }
  if (n > number) {
    res.value = '猜小了'
    num.value = ''
  } else if (n < number) {
    res.value = '猜大了'
    num.value = ''
  } else {
    res.value = '猜对了！'
    n = parseInt(Math.random() * 100) + 1;
    console.log("答案:" + n)
  }

}


</script>


<style scoped>

</style>